package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.umakr.ax.common.enm.AttentionFrom;
import com.umakr.ax.common.mapper.SeekHelpInfoMapper;
import com.umakr.ax.common.model.Msg;
import com.umakr.ax.common.model.SeekHelpInfo;
import com.umakr.ax.common.vo.SeekHelpInfoVO;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 求助信息 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class SeekHelpInfoService extends BaseService<SeekHelpInfoMapper, SeekHelpInfo>{

    @Autowired
    MsgService msgService;
    @Autowired
    RadioStationFollowerService radioStationFollowerService;
    @Autowired
    Environment env;

    private static final String OSS_IMG_PATH = "oss.img.path";
    private static final String SEEK_VOICE = "seekVoice";
    private static final String RESULT = "result";
    private static final String PHOTO_INFO = "photoInfo";

    public Map<String,Object> save(Long userId, Long rsId, String seekerName, String seekTitle, String seekDes, String seekVoice, String addrCoordinate, String addrStr,
                       String contacts, String contactsTell, Integer seekType, String photoInfo,Integer radioLen,String drivingDirection) {
        String aliUrl = env.getProperty(OSS_IMG_PATH);
        int errorCode = 0 ;
        String msg;
        Map<String,Object> data = Maps.newLinkedHashMap();
        Date date = new Date();

        SeekHelpInfo seekHelpInfo = new SeekHelpInfo();
        seekHelpInfo.setUserId(userId);
        seekHelpInfo.setRsId(rsId);
        seekHelpInfo.setSeekerName(seekerName);
        seekHelpInfo.setSeekTitle(seekTitle);
        seekHelpInfo.setSeekDes(seekDes);
        seekHelpInfo.setSeekVoice(seekVoice);
        seekHelpInfo.setAddrCoordinate(addrCoordinate);
        seekHelpInfo.setAddrStr(addrStr);
        seekHelpInfo.setContacts(contacts);
        seekHelpInfo.setContactsTell(contactsTell);
        // 求助类型: 1 寻人 2 寻物 3 车辆求助 4 寻找失主 5 其他
        seekHelpInfo.setSeekType(seekType);
        //此处数据库的类型为Json 但是我们实际上java使用的是String 使用这种方式存储数据会报错
        seekHelpInfo.setPhotoInfo(photoInfo);
        seekHelpInfo.setCreateTime(date);
        seekHelpInfo.setDelStatus(0);
        seekHelpInfo.setDealStatus(0);
        seekHelpInfo.setReviewStatus(0);
        seekHelpInfo.setPushStatus(0);
        seekHelpInfo.setRadioLen(radioLen);
        Long count = baseMapper.save(seekHelpInfo);
        if(count >= 1){
            data.put("id",seekHelpInfo.getId());
            data.put("userId",seekHelpInfo.getUserId());
            data.put("rsId",seekHelpInfo.getRsId());
            data.put("seekerName",seekHelpInfo.getSeekerName());
            data.put("seekTitle",seekHelpInfo.getSeekTitle());
            data.put("seekDes",seekHelpInfo.getSeekDes());
            //设置求助语音消息路径
            if(seekHelpInfo.getSeekVoice() == null || "".equals(seekHelpInfo.getSeekVoice().replace("",""))){
                data.put(SEEK_VOICE,"");
            }else {
                data.put(SEEK_VOICE,aliUrl+seekHelpInfo.getSeekVoice());
            }
            data.put("addrCoordinate",seekHelpInfo.getAddrCoordinate());
            data.put("addrStr",seekHelpInfo.getAddrStr());
            data.put("contacts",seekHelpInfo.getContacts());
            data.put("contactsTell",seekHelpInfo.getContactsTell());
            data.put("seekType",seekHelpInfo.getSeekType());
            //处理图片的链接地址
            data.put(PHOTO_INFO, FormatPhotoUrlUtil.formatStringArray(aliUrl,seekHelpInfo.getPhotoInfo()));
            data.put("createTime",seekHelpInfo.getCreateTime());
            data.put("delStatus",seekHelpInfo.getDelStatus());
            data.put("dealStatus",seekHelpInfo.getDealStatus());
            data.put("reviewStatus",seekHelpInfo.getReviewStatus());
            data.put("pushStatus",seekHelpInfo.getPushStatus());
            data.put("radioLen",seekHelpInfo.getRadioLen());
            Msg msgInfo = new Msg();
            msgInfo.setMsgTime(date);
            //求助类型: 1 寻人 2 寻物 3 车辆求助 4 寻找失主 5 其他
            //消息类型:  0 互动  1 寻人 2 寻物 3 车辆求助 4 寻找失主 5 其他 需要根据求助类型判断
            msgInfo.setMsgType(seekHelpInfo.getSeekType());

            //目前统一发布求助信息只冗余标题信息
            msgInfo.setMsgContentType(0);
            msgInfo.setMsgAttachmentAddr(seekHelpInfo.getSeekTitle());
            msgInfo.setUserId(seekHelpInfo.getUserId());
            msgInfo.setRsId(seekHelpInfo.getRsId());
            //消息方向 暂时不设置
            msgInfo.setMsgDirect(0);

            msgInfo.setStatus(0);
            msgInfo.setRadioLen(seekHelpInfo.getRadioLen());
            //一键求助
            msgInfo.setDrivingDirection(drivingDirection);
            msgInfo.setPositionCoor(seekHelpInfo.getAddrCoordinate());
            msgInfo.setPositionName(seekHelpInfo.getAddrStr());
            //消息来源：0 微信 1 app  2 平台发布 3 webApp
            int fromType = -2;
            if(userId == fromType){
                msgInfo.setMessageFrom(3);
            } else {
                msgInfo.setMessageFrom(1);
                radioStationFollowerService.autoAttention(userId,rsId, AttentionFrom.FAVORITE);
            }
            msgInfo.setHelpId(seekHelpInfo.getId());
            msgInfo.setDelStatus(0);
            msgService.update(msgInfo);
            msg = "发布成功";
            errorCode = 1;
        }else {
            msg = "发布失败";
            errorCode = 0;
        }
        return  resultMap(data,errorCode,msg);
    }

    public Object detail(Long id) {
        String aliUrl = env.getProperty(OSS_IMG_PATH);
        int errorCode = 1 ;
        String msg = "查询成功";
        Map<String,Object> result = baseMapper.detail(id);
        result.put(SEEK_VOICE,result.get(PHOTO_INFO) == null ? "" : aliUrl+result.get(PHOTO_INFO));
        result.put(PHOTO_INFO,FormatPhotoUrlUtil.formatStringArray(aliUrl, (String) result.get(PHOTO_INFO)));

        return resultMap(result,errorCode,msg);
    }

    public Page<SeekHelpInfoVO> load(Integer type, Long userId) {
        String aliUrl = env.getProperty(OSS_IMG_PATH);
        Page<SeekHelpInfoVO> seekHelpInfoVOPage = null;
        if (type == 0) {
            seekHelpInfoVOPage = baseMapper.load();
        } else {
            seekHelpInfoVOPage = baseMapper.loadSelf(userId);
        }
        for (SeekHelpInfoVO item : seekHelpInfoVOPage.getRecords()){
            //设置链接地址
            if(item.getMsgDirect() == 2){
                item.setHeadimgurl(aliUrl+item.getRsLogo());
            }else{
                item.setHeadimgurl(FormatPhotoUrlUtil.formatString(aliUrl,item.getHeadimgurl()));
            }

            item.setPhotoInfo(FormatPhotoUrlUtil.formatStringArray(aliUrl,item.getPhotoInfo()));
            //设置求助语音消息路径
            if(item.getSeekVoice() == null || "".equals(item.getSeekVoice().replace(" ",""))){
                item.setSeekVoice("");
            }else {
                item.setSeekVoice(aliUrl+item.getSeekVoice());
            }

            if(item.getSeekType() == 0){
                item.setSeekTypeName("互动");
            }else if(item.getSeekType() == 1){
                item.setSeekTypeName("寻人");
            }else if(item.getSeekType() == 2){
                item.setSeekTypeName("寻物");
            }else if(item.getSeekType() == 3){
                item.setSeekTypeName("车辆求助");
            }else if(item.getSeekType() == 4){
                item.setSeekTypeName("寻找失主");
            }else if(item.getSeekType() == 5){
                item.setSeekTypeName("其他");
            }
        }

        return seekHelpInfoVOPage;
    }

    public Object updateSeekHelpInfoPushStatus(Long id, Integer pushStatus) {
        int errorCode = 0 ;
        String msg ;
        Map<String,Object> data = Maps.newLinkedHashMap();
        baseMapper.updateSeekHelpInfoPushStatus(id, pushStatus);
        errorCode = 1 ;
        msg = "操作成功";
        data.put(RESULT,1);
        return resultMap(data,errorCode,msg);
    }

    public Map<String,Object> updateSeekHelpInfoDealStatus(Long id, Integer dealStatus) {
        int errorCode = 0 ;
        String msg = "操作失败";
        Map<String,Object> data = Maps.newLinkedHashMap();
        data.put(RESULT,0);
        if(dealStatus >= 0 && dealStatus <= 1 ){
            baseMapper.updateSeekHelpInfoDealStatus(id, dealStatus);
            errorCode = 1 ;
            msg = "操作成功";
            data.put(RESULT,1);
        }

        return resultMap(data,errorCode,msg);
    }

    public Map<String,Object> delSeekHelpInfo(Long id, Integer delStatus) {
        int errorCode = 0 ;
        String msg = "操作失败";
        Map<String,Object> data = Maps.newLinkedHashMap();
        data.put(RESULT, 0);
        if(delStatus >= 0 && delStatus <= 1 ) {
            baseMapper.delSeekHelpInfo(id, delStatus);
            //软删除求助信息列表后，消息列表中的消息也要软删除
            EntityWrapper<Msg> msgWrapper = new EntityWrapper<>();
            msgWrapper.eq("help_id", id);
            Msg resultMsg = msgService.selectOne(msgWrapper);
            if (resultMsg != null && resultMsg.getDelStatus() == 0) {
                msgService.delMsg(resultMsg.getId());
            }
            errorCode = 1;
            msg = "操作成功";
            data.put(RESULT, 1);
        }
        return resultMap(data,errorCode,msg);
    }
}
